###############################################################################
# Copyright (C) 2010 Chris Wroe
#
# Modifications to the initial code base are copyright of their respective
# authors, or their employers as appropriate.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307
##############################################################################
require 'yaml'
require "load_snomed_concept_component"
require "load_snomed_description_component"
require "load_snomed_relationship_component"
require "rubygems"
require "neo4j"
require "model"


@release_node=nil

class SnomedReleaseLoader

  attr_accessor :release_node, :module_node, :concept_loader, :description_loader, 
    :relationship_loader,:load_config,:version_property

  def initialize(file_name)
    begin
      self.load_config= YAML.load_file file_name
    rescue
      puts 'parsing error'
      raise
    end

    self.concept_loader = LoadSnomedConceptComponent.new(@load_config['concept_format_file'])
    self.description_loader = LoadSnomedDescriptionComponent.new(@load_config['description_format_file'])
    self.relationship_loader = LoadSnomedRelationshipComponent.new(@load_config['relationship_format_file'])
    self.version_property={'version'=>''}
  end


  def create_release_node
    Neo4j::Transaction.new

    hits = Release.find(self.load_config['release'])
    raise 'duplicate release' if not hits.empty?

    self.release_node=r=Release.new
    r.name=self.load_config['release']
    Neo4j::Transaction.finish
    @version_property['version'] << r.name
  end

  def create_module_node(module_config)
    Neo4j::Transaction.new
   
    self.module_node=m=ComponentModule.new
    
    m.name=module_config['name']
    m.release=self.release_node
    Neo4j::Transaction.finish
    @version_property['version'] << ':'+ m.name
  end

  def load_release
    self.create_release_node
    self.load_modules
  end

  def load_modules

    self.load_config['modules'].each do |config|
      self.create_module_node(config)
      
      Neo4j.stop
      self.load_concept_files(config)
      self.load_relationship_files(config)
      self.load_description_files(config)

      Neo4j.start
    end


  end

  def load_concept_files(module_config)

    count  = 0
    
    module_config['concept_files'].each do |file_name|
      f = File.open(module_config['root']+file_name)
      self.concept_loader.module_config=module_config
      count += self.concept_loader.batch_insert_from_file(f,@version_property)
      f.close
    end
    return count
  end

  def load_relationship_files(module_config)
    count =0
    module_config['relationship_files'].each do |file_name|
      f = File.open(module_config['root']+file_name)
      self.relationship_loader.module_config=module_config
      count+= self.relationship_loader.batch_insert_from_file(f,@version_property)
      f.close
    end
    return count
  end

  def load_description_files(module_config)

    
    count =0
    module_config['description_files'].each do |file_name|
      f = File.open(module_config['root']+file_name)
      self.description_loader.module_config=module_config
      count += self.description_loader.batch_insert_from_file(f,@version_property)
      f.close
    end
    return count
  end
end
